<?php

namespace App\Http\Controllers;

use App\Exceptions\ApiException;
use App\Http\Requests\PhotoRequest;
use App\Http\Responses\ApiResponse;
use App\Models\Photo;
use Illuminate\Http\Request;

class PhotosController extends Controller
{

    public function index(){

        return ApiResponse::success(Photo::all()->toArray());
    }


    public function create(){

        return  view("photos/create_and_edit");
    }


    public function store(PhotoRequest $request){

        $model = Photo::create($request->all());

        if(!$model){
            throw  new ApiException('添加数据失败');
        }

        return ApiResponse::success(true);
    }

    public function show(PhotoRequest $request, $id){

        if($request->ajax()){
            $model = Photo::find($id);

            if(empty($model)){
                throw  new ApiException('没有该数据');
            }

            return ApiResponse::success($model->toArray());
        }


        return view("photos/show");
    }

    public function edit(){
        return 'edit';
    }

    public function update(){
        return 'update';
    }

    public function destroy(){
        return 'destroy';
    }
}
